package org.fastsyncer.manager.health;

import org.fastsyncer.common.constant.ConnectorConstant;
import org.fastsyncer.common.entity.DatabaseConfig;
import org.fastsyncer.common.entity.LdapConfig;
import org.fastsyncer.common.entity.Mapping;
import org.fastsyncer.common.entity.WebServiceConfig;
import org.fastsyncer.connector.database.DatabaseConnector;
import org.fastsyncer.connector.framework.ConnectorFactory;
import org.fastsyncer.connector.ldap.LdapConnector;
import org.fastsyncer.connector.ws.WebServiceConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HealthMonitor {

	private final Logger logger = LoggerFactory.getLogger(HealthMonitor.class);

	private static HealthMonitor instance = new HealthMonitor();

	private HealthMonitor() {
	}

	public static HealthMonitor getInstance() {
		return instance;
	}

	/**
	 * 测试驱动连接器配置是否可用
	 * @Title: test 
	 * @Description: 测试驱动连接器配置是否可用
	 * @param mapping 连接器配置
	 * @return: boolean 返回true/false
	 */
	public boolean test(Mapping mapping) {
		boolean r = false;
		try {
			String connector = mapping.getConnector();
			switch (connector) {
			case ConnectorConstant.DB_CONNECTOR:
				DatabaseConnector dbConnector = (DatabaseConnector) ConnectorFactory
						.getInstance(ConnectorConstant.DB_CONNECTOR);
				DatabaseConfig dbConfig = (DatabaseConfig) mapping.getConfig();
				r = dbConnector.isAlive(dbConfig);
				break;
			case ConnectorConstant.WS_CONNECTOR:
				WebServiceConnector wsConnector = (WebServiceConnector) ConnectorFactory
						.getInstance(ConnectorConstant.WS_CONNECTOR);
				WebServiceConfig wsConfig = (WebServiceConfig) mapping.getConfig();
				r = wsConnector.isAlive(wsConfig);
				break;
			case ConnectorConstant.LDAP_CONNECTOR:
				LdapConnector ldapConnector = (LdapConnector) ConnectorFactory
						.getInstance(ConnectorConstant.LDAP_CONNECTOR);
				LdapConfig ldapConfig = (LdapConfig) mapping.getConfig();
				r = ldapConnector.isAlive(ldapConfig);
				break;
			default:
				break;
			}
		} catch (Exception e) {
		    logger.error("An exception occurred while checking the drive:" + e.getMessage(), e.getClass());
		}
		return r;
	}

}
